<?php

/**
 * CollectionProperty form.
 *
 * @package    travide
 * @subpackage form
 * @author     Travide Project Group
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class CollectionPropertyForm extends BaseCollectionPropertyForm {

    public function postValidateFields($validator, $values) {
        /**
         * Check is a value was supplied for the property but a property detail
         * was not selected
         */
        if (($values['propertyValue'] != '') && ($values['propertyDetailUin'] == '')) {
            throw new sfValidatorError($validator, 'Property value specified without specifying property type');
        }

        return $values;
    }

    public function configure() {
        /**
         * Set the widget formatter to InlineTable
         */
        $this->getWidgetSchema()->addFormFormatter('InlineTable', new InlineTableFormSchemaFormatter($this->getWidgetSchema()));
        $this->getWidgetSchema()->setFormFormatterName('InlineTable');
        /**
         * For new objects (i.e. blank forms) allow an empty option to force
         * the user to select - or leave blank so some forms can interpret this
         * as ignore or delete
         */
        $this->getValidatorSchema()->setPostValidator(new sfValidatorCallback(array('callback' => array($this, 'postValidateFields'))));

        /**
         * Configure Property Detail Field
         */
        $w = $this->getWidget('propertyDetailUin');
        $w->setOption('add_empty', $this->isNew());
        $w->setOption('table_method', 'getPropertyDetailListQuery');

        $w->setLabel('Property');
        $this->getValidator('propertyDetailUin')->setOption('required', !$this->isNew());
        /**
         * Configure Property Value Field
         */
        $w = new sfWidgetFormTextarea();
//        $w = $this->getWidget('propertyValue');
        $w->setLabel('Value');
        $w->setAttribute('class', 'propertyValue');
        $this->setWidget('propertyValue', $w);
        $this->getValidator('propertyValue')->setOption('required', !$this->isNew());

        if (!$this->isNew) {

            /**
             * Add the Delete Property Widget - propertyDelete
             * Allows user to select to delete the property on edit
             */
            $w = new sfWidgetFormInputCheckbox();
            $w->setLabel('Delete');
            $this->setWidget('propertyDelete', $w);
            $this->setValidator('propertyDelete', new sfValidatorBoolean());
        }
        /**
         * Remove thisCollectionUin - as we don't want this to show up on the
         * new or edit forms for Collections - it will be set automatically
         */
        unset($this['collectionUin']);
    }

}
